package com.gao.security.autoconfig.verificationcode;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gao.security.verificationcode.common.BaseVo;
import com.gao.security.verificationcode.common.ResponseCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>
 *     全局无权限处理方案
 * </p>
 *
 * @author GaoShuangWen
 */
public class GlobalAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        response.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter printWriter = response.getWriter();
        ObjectMapper objectMapper =new ObjectMapper();
        printWriter.append(
                objectMapper.writeValueAsString(BaseVo.failure(ResponseCode.PERMISSION_NO_ACCESS)));
    }

}
